package pers.lolicer.redtidelibrary.components

import AppColor
import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import pers.lolicer.redtidelibrary.R

@Composable
fun BookNavigation(sortMethod: MutableIntState, foldersSet: Set<String>, folderFilter: MutableState<String>){
    Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth()){
        ClassificationBar(foldersSet, folderFilter)
        SortBar(sortMethod)
    }

}

// 分类 下拉菜单
@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun ClassificationBar(foldersSet: Set<String>, folderFilter: MutableState<String>){
    val context = LocalContext.current
    var expanded by remember { mutableStateOf(false) }

    val scope = rememberCoroutineScope()

    Box(modifier = Modifier){
        Button(
            onClick = {expanded = !expanded},
            shape = RectangleShape,
            colors = AppColor.DropdownButton(context)
        ) {
            Text(text =
                if(folderFilter.value == "")
                    "全部"
                else
                    folderFilter.value
            )
            Icon(imageVector = if(expanded) Icons.Default.KeyboardArrowUp else Icons.Default.KeyboardArrowDown, contentDescription = null)
        }
        DropdownMenu(
            expanded = expanded,
            onDismissRequest = {expanded = false},
            modifier = Modifier.background(AppColor.DropDownMenuBackground(context))
        ) {
            if(folderFilter.value != ""){
                DropdownMenuItem(
                    text = { Text("全部")},
                    colors = AppColor.DropdownItem(context),
                    onClick = {
                        expanded = false
                        scope.launch {
                            delay(100) // 等动画执行完再进行后续操作，下面三个同理
                            folderFilter.value = ""
                        }
                    }
                )
            }

            foldersSet.forEach { folderName ->
                DropdownMenuItem(
                    text = { Text(text = folderName)},
                    colors = AppColor.DropdownItem(context),
                    onClick = {
                        expanded = false
                        scope.launch {
                            delay(100)
                            folderFilter.value = folderName
                        }
                    }
                )
            }
        }
    }
}

// 排序方式 下拉菜单
@Composable
fun SortBar(sortMethod: MutableIntState){
    val context = LocalContext.current
    var expanded by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()

    Box(modifier = Modifier){
        Button(
            onClick = {expanded = !expanded},
            shape = RectangleShape,
            colors = AppColor.DropdownButton(context)
        ) {
            Text(text = when(sortMethod.intValue){
                    R.integer.sortMethod_time_import -> "导入时间"
                    R.integer.sortMethod_time_last_open -> "打开时间"
                    else -> throw Exception("Error: 无法识别的排序情况！")
                }
            )
            Icon(imageVector = if(expanded) Icons.Default.KeyboardArrowUp else Icons.Default.KeyboardArrowDown, contentDescription = null)
        }
        DropdownMenu(
            expanded = expanded,
            onDismissRequest = {expanded = false},
            modifier = Modifier.background(AppColor.DropDownMenuBackground(context))
        ) {
            DropdownMenuItem(
                text = { Text(text = "导入时间")},
                colors = AppColor.DropdownItem(context),
                onClick = {
                    expanded = false
                    scope.launch{
                        delay(150)
                        sortMethod.intValue = R.integer.sortMethod_time_import
                    }

                }
            )
            DropdownMenuItem(
                text = { Text(text = "打开时间")},
                colors = AppColor.DropdownItem(context),
                onClick = {
                    expanded = false
                    scope.launch{
                        delay(150)
                        sortMethod.intValue = R.integer.sortMethod_time_last_open
                    }
                }
            )
        }
    }
}